from django.db import models
from django.core import validators
from django.utils.functional import curry
from django.conf import settings
from nameremoved.helpers import ImageSizeValidator
from nameremoved.comics.fields import RenamingImageField
import datetime, os, shutil

class Contributor(models.Model):
    name = models.CharField(max_length=100)
    info = models.TextField()
    
    def __unicode__(self):
        return u"%s" % self.name
    
    class Meta:
        ordering = ['name']
    
    class Admin:
        pass

class Contribution(models.Model):
    contribution_type_list = (
        ('suggested', 'suggested'),
        ('drew', 'drew'),
        ('wrote', 'wrote'),
        ('photographed', 'photographed'),
        ('modelled','modelled'),
        ('fourth_panelled', 'fourth panelled'),
        ('guested','guest comicked'),
    )
    
    contribution_type = models.SlugField(
        choices=contribution_type_list,
        default="suggested"
    )
    contributor = models.ForeignKey(Contributor, null=True, blank=True)
    aka = models.CharField(
        max_length=100,
        verbose_name="suggested by",
        help_text="Example: Daniel C. Dennett"
    )
    content = models.TextField(
        verbose_name="suggestion",
        help_text="Example: Rabid eat all the sandwiches now he cry."
    )
    submitted = models.DateField(auto_now_add=True)
    flagged = models.BooleanField(default=False)
    
    def is_contributor(self):
        if self.contributor:
            return True
        else:
            return False
    is_contributor.boolean = True
    
    def __unicode__(self):
        return u"%s %s %s" % (self.aka, self.contribution_type, self.content)
    
    class Admin:
        list_display = (
            'submitted',
            'aka',
            'contribution_type',
            'content',
            'flagged',
            'is_contributor'
        )
        list_filter = (
            'flagged',
            'contribution_type', 
            'contributor'
        )
    
    def get_absolute_url(self):
        return '/contribute/#%d' % self.id

class PublicComicsManager(models.Manager):
    def get_query_set(self):
        return super(PublicComicsManager, self).get_query_set().filter(date__lte=datetime.date.today())

class Comic(models.Model):
    """a single comic in the series of comics"""
    comics = models.Manager()
    public_comics = PublicComicsManager()
    
    date = models.DateField(unique=True)
    title = models.CharField(max_length=100)
    transcript = models.TextField()
    newstitle = models.CharField(max_length=100, verbose_name="news title")
    news = models.TextField()
    comic = RenamingImageField(
        upload_to="comic_files/",
        height_field='comic_height',
        width_field='comic_width',
        validator_list=[curry(ImageSizeValidator, width=550)],
    )
    comic_height = models.IntegerField(blank=True, null=True)
    comic_width = models.IntegerField(blank=True, null=True,
                    help_text="The width of a comic should always be 550px.")
    origin = models.ManyToManyField(
        Contribution,
        limit_choices_to={'flagged': True},
        null=True, blank=True,
        filter_interface=models.VERTICAL
    )
    starts_storyline = models.BooleanField(default=False)
    storyline_title = models.CharField(max_length=100,
                                       verbose_name="storyline title",
                                       blank=True)
    storyline_description = models.TextField(verbose_name="storyline description", blank=True)
    
    class Meta:
        ordering = ['date']
    
    class Admin:
        ordering = ['-date']
        list_display = ('title', 'date', 'is_public', 'starts_storyline')
        list_filter = ('starts_storyline', )
        list_display_links = ('date', 'title')
        search_fields = ('@title', '@transcript')
        date_hierarchy = 'date'
        fields = (
            (None,
                {'fields': ('date', 'title', 'comic',
                            ('comic_height', 'comic_width'))}),
            ("Transcription", {'fields': ('transcript',)}),
            ("News", {'fields': ('newstitle', 'news')}),
            ("Suggestion", {'fields': ('origin',), 'classes': 'collapse'}),
            ("Storyline",
                {'fields': (('starts_storyline', 'storyline_title'),
                    'storyline_description'),
                    'classes': 'collapse'})
        )
    
    def __unicode__(self):
        return u"%s - %s" % (self.title, self.date)
    
    def is_public(self):
        """used in admin view"""
        return self.date <= datetime.date.today()
    is_public.boolean = True
    
    @models.permalink
    def get_absolute_url(self):
        return ('nameremoved.comics.views.comic', (),
                {'which_comic':         
                    Comic.comics.filter(date__lte=self.date).count()})

